package com.liuceng.zen.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.liuceng.zen.constants.Constants;
import com.liuceng.zen.pojo.domain.FrontAdGroup;
import com.liuceng.zen.pojo.domain.FrontCreative;
import com.liuceng.zen.pojo.domain.FrontMaterial;
import com.liuceng.zen.pojo.form.FrontMaterialForm;
import com.liuceng.zen.service.FrontAdGroupService;
import com.liuceng.zen.service.FrontCreativeService;
import com.liuceng.zen.service.FrontMaterialService;
import com.liuceng.zen.service.GdtCreativeService;

/**
 * OriginalityController.java 广告创意库相关
 * 
 * @author wangyanwen
 *
 */
@Controller
public class CreativeController extends BaseController {
  @Autowired
  GdtCreativeService gdtCreativeService;

  @Autowired
  FrontMaterialService frontMaterialService;

  @Autowired
  FrontCreativeService frontCreativeService;
  @Autowired
  FrontAdGroupService frontAdGroupService;

  // 广告创意/orig/ad
  @RequestMapping(value = "/materials/list", method = RequestMethod.GET)
  public String adCreative(@ModelAttribute("form") FrontMaterialForm form) {
    form.setMemberId(getCurrentUserId());
    form.setPage(frontMaterialService.findPage(form));
    return "creatives/list";
  }

  // @RequestMapping(value = "/common/check_creatives", method = RequestMethod.GET)
  // public String ajaxCreativesPage(@ModelAttribute("form") AdCreativesForm form) {
  // form.setPage(gdtCreativeService.findPage(form));
  // return "ad/creatives";
  // }

  @RequestMapping(value = "/materials/add", method = RequestMethod.POST)
  public String add(@ModelAttribute("form") FrontMaterialForm form,
      RedirectAttributes redirectAttributes) {
    FrontMaterial frontMaterial = new FrontMaterial();
    frontMaterial.setSitesetType(form.getSitesetType());
    frontMaterial.setMemberId(getCurrentUserId());
    frontMaterial.setCrtSize(form.getCrtSize());
    frontMaterial.setPicPath(form.getPicPath());
    frontMaterial.setThumbPicPath(form.getThumbPicPath());
    frontMaterial.setImageId(form.getImageId());
    frontMaterial.setName(form.getName());
    frontMaterial.setCrtSizeDetail(form.getCrtSizeDetail());
    frontMaterial.setCreatedAt(new Date());
    frontMaterial.setUpdatedAt(new Date());
    frontMaterialService.insert(frontMaterial);
    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "广告创意添加成功");
    return "redirect:/materials/list";
  }

  @RequestMapping(value = "/ad_group/materials/add", method = RequestMethod.POST)
  public @ResponseBody String addMaterial(@ModelAttribute("form") FrontMaterialForm form,
      RedirectAttributes redirectAttributes) {
    FrontMaterial frontMaterial = new FrontMaterial();
    frontMaterial.setSitesetType(form.getSitesetType());
    frontMaterial.setMemberId(getCurrentUserId());
    frontMaterial.setCrtSize(form.getCrtSize());
    frontMaterial.setPicPath(form.getPicPath());
    frontMaterial.setThumbPicPath(form.getThumbPicPath());
    frontMaterial.setImageId(form.getImageId());
    frontMaterial.setName(form.getName());
    frontMaterial.setCrtSizeDetail(form.getCrtSizeDetail());
    frontMaterial.setCreatedAt(new Date());
    frontMaterial.setUpdatedAt(new Date());
    frontMaterialService.insert(frontMaterial);

    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "广告创意添加成功");
    // Map<String, Object> filterMap = new HashMap<String, Object>();
    // filterMap.put("succsss", "success");
    return "success";
  }

  @RequestMapping(value = "/materials/edit", method = RequestMethod.POST)
  public String edit(@ModelAttribute("form") FrontMaterialForm form,
      RedirectAttributes redirectAttributes) {
    FrontMaterial frontMaterial = new FrontMaterial();
    frontMaterial.setId(form.getId());
    frontMaterial.setSitesetType(form.getSitesetType());
    frontMaterial.setCrtSize(form.getCrtSize());
    frontMaterial.setPicPath(form.getPicPath());
    frontMaterial.setThumbPicPath(form.getThumbPicPath());
    frontMaterial.setImageId(form.getImageId());
    frontMaterial.setName(form.getName());
    frontMaterial.setCrtSizeDetail(form.getCrtSizeDetail());
    frontMaterial.setUpdatedAt(new Date());
    frontMaterialService.update(frontMaterial);

    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "广告创意修改成功");
    return "redirect:/materials/list";
  }

  // @RequestMapping(value = "/creatives/delete/{id}", method = RequestMethod.GET)
  // public String delete(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
  // frontMaterialService.delete(id);
  // redirectAttributes.addFlashAttribute(Constants.OK_MSG, "广告创意删除成功");
  // return "redirect:/materials/list";
  // }

  @RequestMapping(value = "/creatives/delete/{id}", method = RequestMethod.POST)
  public @ResponseBody String delete(@PathVariable Integer id,
      RedirectAttributes redirectAttributes) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("frontMaterialId", id);
    map.put("memberId", getCurrentUserId());
    List<FrontCreative> frontCreatives = frontCreativeService.findFrontCreatives(map);


    if (frontCreatives != null && frontCreatives.size() > 0) {

      for (int i = 0; i < frontCreatives.size(); i++) {
        map.put("id", frontCreatives.get(i).getFrontAdGroupId());
        List<FrontAdGroup> frontAdGroups = frontAdGroupService.search(map);
        if (frontAdGroups != null && frontAdGroups.size() > 0) {

          for (int j = 0; i < frontAdGroups.size(); j++) {
            if (frontAdGroups.get(j).getFrontAdGroupType() != 2) {
              // 删除物料
              frontMaterialService.delete(id);
              // 删除创意
              frontCreativeService.delete(frontCreatives.get(i).getId());
              return "success";
            } else {
              return "false";
            }
          }

        } else {
          frontMaterialService.delete(id);
          frontCreativeService.delete(frontCreatives.get(i).getId());
          return "success";
        }
      }

    } else {
      frontMaterialService.delete(id);
      return "success";
    }
    frontMaterialService.delete(id);
    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "广告创意删除成功");
    return "success";
  }
}
